<!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-04-07 20:18:30
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-08 17:32:54
-->
<template>
    <div class="app-container">
        <div class="tech-border-out">
            <div class="tech-border">
                <el-card>
                    <div slot="header" class="clearfix">
                            <dv-decoration-7 style="width:150px;height:30px;;"> <p style="padding: 0 15px;">客户管理</p>  </dv-decoration-7>
                    </div>
                    <div class="filter-container">
                            <el-input v-model="listQuery.name" placeholder="请输入客户名称" clearable style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" @keyup.enter.native="handleFilter" />
                            <el-input v-model="listQuery.contact_name" placeholder="请输入联系人" clearable style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" @keyup.enter.native="handleFilter" />
                            <el-input v-model="listQuery.contact_phone" placeholder="请输入联系方式" clearable style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" @keyup.enter.native="handleFilter" />

                        <el-button v-waves  class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                        {{ '搜索' }}
                        </el-button>
                        <el-button v-waves :disabled="!checkPermission(['customer.create'])"  class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
                            {{ "添加" }}
                        </el-button>  

                        <el-button v-waves :disabled="!checkPermission(['customer.export'])"  type="success" icon="el-icon-upload2" @click="handleExport">
                            {{ '导出' }}
                        </el-button>

                    </div>
                    <el-table
                        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                        :data="list"
                        border
                        fit
                        class="table-container"
                        highlight-current-row
                        style="width: 100%"
                        size="medium"
                        >  
                        
                      <el-table-column  align="center" label="客户名称">
                        <template slot-scope="scope">
                            <span>{{ scope.row.name }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column  align="center" label="联系人">
                        <template slot-scope="scope">
                            <span>{{ scope.row.contact_name }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column  align="center" label="联系方式">
                        <template slot-scope="scope">
                            <span>{{ scope.row.contact_phone }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column  align="center" label="联系地址">
                        <template slot-scope="scope">
                            <span>{{ scope.row.contact_address }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column  align="center" label="所属行业">
                        <template slot-scope="scope">
                            <span>{{ scope.row.industry }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" width="250" :label="'操作'">
                        <template slot-scope="scope">
                            <!-- <el-button v-waves :disabled="!checkPermission(['customer.detail'])" size='mini'  type="info" icon="el-icon-info" @click="handleDetail(scope.$index, scope.row)">
                                {{ "详情" }}
                            </el-button> -->
                            <el-button v-waves v-permission="['customer.edit']"  type="primary"  size='mini' icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
                                {{ "编辑" }}
                            </el-button>
                            <el-button v-waves v-permission="['customer.delete']"  type="danger" :destroy-on-close="true" size='mini' icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
                                {{ "删除" }}
                            </el-button>
                        </template>
                    </el-table-column>
                    </el-table>
                    <dv-decoration-10 :dur="1" style="height:5px;" />
                    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
                </el-card>
            </div>
            
        </div>
            <el-dialog   width="650px"  :visible.sync="detail.dialogVisible"  :show-close="false" v-if="detail.dialogVisible">
                <dv-border-box-11  class="el-new-dialog" title="客户详情">
                    <detail :data="detail.data" />
                </dv-border-box-11>
            </el-dialog>
            <el-dialog  width="650px"  :visible.sync="edit.dialogVisible" :show-close="false" @close="getList">
                <dv-border-box-11  class="el-new-dialog" title="编辑客户">
                    <edit :item="edit" />
                </dv-border-box-11>
          </el-dialog>
          <el-dialog  width="650px" :visible.sync="create.dialogVisible"  :show-close="false" @close="getList">
            <dv-border-box-11  backgroundColor="#0C213E" class="el-new-dialog" title="添加客户">
                <create :item="create" />
            </dv-border-box-11>
          </el-dialog>

    </div>
</template>
<script>
    import waves from '@/directive/waves' // waves directive
    import permission from '@/directive/permission/index.js' // 权限判断指令
    import checkPermission from '@/utils/permission' // 权限判断函数
    import { list, detail, del } from '@/api/customer' // api接口
    import { parseTime } from '@/utils' // 时间格式化函数
    import { getToken } from '@/utils/auth' // 获取token
    import Pagination from '@/components/Pagination' // 分页组件
    import Create from './components/create' // 添加组件  
    import Detail from '@/components/Detail/detail' // 详情组件 
    import Edit from './components/edit'  // 编辑组件  

    export default{
        name: 'customerIndex',
        components: { Pagination,Create,Detail,Edit }, // 引入组件
        filters: {
            parseTime // 时间过滤器
        },
        directives: { waves, permission }, // 引入指令
          data() {
            return {
             headerObj: {
                'Authorization': 'Bearer ' + getToken()
            },
            list: [], // 列表数据
            total: 0, // 总条数
            select_ids : [], // 选中的id集合
            listQuery: { //列表查询参数
                page: 1,
                limit: 10
            },
             create: { // 新增
                 dialogVisible: false
                },  
            detail: { // 详情
                   dialogVisible: false,
                   data: []
                },  
            edit: { // 编辑
                 dialogVisible: false,
                 id: ''
            },  

            }
        },
          created() {
            this.getList()
        },
        methods: {
            checkPermission, // 权限判断函数
            handleFilter() { // 查询
                this.listQuery.page = 1
                this.getList()
            },
            getList() { // 获取列表
                list(this.listQuery).then(response => {
                        this.list = response.data.data
                        this.total = response.data.total
                    })
                },
        handleCreate() { // 新增对话框显示
            this.create.dialogVisible = true
        },  
        handleDetail(index, row) { // 详情
            detail(row.id).then((res) => {
                    this.detail.dialogVisible = true
                    const data = res.data
                    this.detail.data = [
                    [                {
                    name:"客户名称",
                    content:data.name,
                    type: "text"
                },
                {
                    name:"联系人",
                    content:data.contact_name,
                    type: "text"
                },
                {
                    name:"联系方式",
                    content:data.contact_phone,
                    type: "text"
                },
                {
                    name:"联系地址",
                    content:data.contact_address,
                    type: "text"
                },
                {
                    name:"所属行业",
                    content:data.industry,
                    type: "text"
                },
]
                    ]
                })
            },  
        handleEdit(index, row) { // 编辑对话框显示
            this.edit.dialogVisible = true
            this.edit.id = row.id
        },  
        handleDelete(index, row) { // 删除
            const thiz = this
            this.$confirm('确认要删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del(row.id).then(() => {
                this.$message({
                    message: '删除成功',
                    type: 'success',
                    duration: 1500,
                    onClose() {
                        thiz.list.splice(index, 1)
                    }
                })
                })
            }).catch(() => {

            })
        },   
    handleExport(){
            let query="";
            for ( let key in this.listQuery) {
                query+= `${key}=${this.listQuery[key]}&`
            }
            window.location.href = process.env.VUE_APP_BASE_API + `admin/customer_export?${query}`;
        },  

            }
    }
</script>

